﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;

namespace MoqContrib.AutoMock.Autofac.Tests
{
	[TestFixture]
	public class InstanceProviderFixture
	{
		private IInstanceProvider _provider;

		#region basic classes and objects used for testing
		public class ConcreteType { }
		public interface ISample { }
		#endregion

		[SetUp]
		public void SetUp()
		{
			_provider = new InstanceProvider();
		}

		[Test]
		public void can_resolve_unregistered_concrete_type_using_GetInstance_with_type_param()
		{
			var concreteType = (ConcreteType)_provider.GetInstance(typeof (ConcreteType));
			Assert.That(concreteType, Is.Not.Null);
		}

		[Test]
		public void can_resolve_unregistered_concrete_type_using_generic_GetInstance()
		{
			var concreteType = _provider.GetInstance<ConcreteType>();
			Assert.That(concreteType, Is.Not.Null);
		}

		[Test]
		public void can_get_instance_of_a_mock_for_an_unregistered_interface()
		{
			var mockedInterface = _provider.GetInstance<ISample>();
			Assert.That(mockedInterface, Is.Not.Null);
			Assert.That(mockedInterface, Is.InstanceOf(typeof(IMocked<ISample>)));
		}

		[Test]
		public void get_instance_returns_different_instances_of_a_concrete_type_for_multiple_requests()
		{
			var concreteType1 = (ConcreteType)_provider.GetInstance(typeof (ConcreteType));
			var concreteType2 = (ConcreteType)_provider.GetInstance(typeof (ConcreteType));
			Assert.AreNotSame(concreteType1, concreteType2);
		}

		[Test]
		public void get_instance_returns_the_same_mock_object_more_than_once()
		{
			var mockedInterface1 = _provider.GetInstance<ISample>();
			var mockedInterface2 = _provider.GetInstance<ISample>();
			Assert.AreSame(mockedInterface1, mockedInterface2);
		}
	}
}
